package edu.gatech.foodaway;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class PreviousItemsActivity extends Activity  {
	TextView txt_rname;
	ImageView img_rpicture;
	TextView txt_sname;
	ListView list_postingorder;
	Button btn_addtolist;
	public static final String[] mockFoodPreviouslyUsedList = {"Hotdogs","Buns","Corns"};
	public static final String RESULT_PREVIOUS = "result_previous";
	Intent resultIntent;
	ArrayList<String> selectedItems = new ArrayList<String>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_previousitems);
		resultIntent = new Intent();
		txt_rname = (TextView) findViewById(R.id.textView_previousitems_restaurantname);
		img_rpicture = (ImageView) findViewById(R.id.imageView_previousitems_restaurantpic);
		txt_sname = (TextView) findViewById(R.id.textView_previousitems);
		list_postingorder = (ListView)findViewById(R.id.listView_previousfooditems);
		btn_addtolist = (Button) findViewById(R.id.btn_addtolist);
		list_postingorder.setItemsCanFocus(false);
		list_postingorder.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		list_postingorder.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, mockFoodPreviouslyUsedList));

		
		btn_addtolist.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {

				SparseBooleanArray a = list_postingorder.getCheckedItemPositions();
				for(int i = 0; i < mockFoodPreviouslyUsedList.length ; i++)
		          {
		          	
		              if (a.valueAt(i))
		              {
		              	selectedItems.add(mockFoodPreviouslyUsedList[i]);
		              	Log.v("checking",i+":"+a.valueAt(i)+selectedItems.get(i)+selectedItems.size());
		           
		              }
		          }
				
				resultIntent.putExtra(RESULT_PREVIOUS, selectedItems);
				Log.v("stuff sent size", ""+selectedItems.size());
            	Log.v("stuff sent",selectedItems.toString());
          	 

				setResult(Activity.RESULT_OK, resultIntent);
				finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.donorhome, menu);
		return true;
	}

}
